﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using JiveMessenger.Resources;
using System.Threading;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using Microsoft.Phone.Controls.Primitives;
using System.Diagnostics;
using JiveMessenger.ViewModels;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Helpers;
using JiveMessenger.Settings;

namespace JiveMessenger.Views
{

    public partial class SettingsPage : PhoneApplicationPage
    {
        public SettingsPage()
        {
            DateTime start = DateTime.Now;
            InitializeComponent();
            //ProgressIndicator prog = new ProgressIndicator();
            //prog.IsIndeterminate = false;
            //prog.Text = "Jive Messenger";
            //prog.IsVisible = true;
            //SystemTray.SetProgressIndicator(this, prog);
            Debug.WriteLine("time to load properties page: {0}ms", DateTime.Now.Subtract(start).TotalMilliseconds);
        }

        private void toggleButton_Checked(object sender, RoutedEventArgs e)
        {
            ToggleSwitch ts = sender as ToggleSwitch;
            ts.Content = "On";
        }

        private void toggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            ToggleSwitch ts = sender as ToggleSwitch;
            ts.Content = "Off";
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {

            if (this.PropertiesPivot.SelectedItem == PushNotificationPropertiesPivotItem)
            {
                this.PropertiesPivot.SelectedItem = GeneralPropertiesPivotItem;
                e.Cancel = true;
            }

            if (this.AddServerPopup != null && this.AddServerPopup.IsOpen)
            {
                this.AddServerPopup.IsOpen = false;
                e.Cancel = true;
                return;
            }
            if (this.pwBoxPassword.Password != null && this.pwBoxPassword.Password != AppSettings.AppSettingsInstance.PasswordSetting)
                AppSettings.AppSettingsInstance.PasswordSetting = StaticMethods.EncodePassword(this.pwBoxPassword.Password);

        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ViewModelLocator.CleanupViewModelSettingsPage();
            base.OnNavigatedFrom(e);
        }

        private void SettingsPageContentControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ViewModelSettingsPage vm = this.DataContext as ViewModelSettingsPage;
            vm.ContentControlDisabledCommand.Execute(e);
        }

        private void AddServerPopup_Opened(object sender, EventArgs e)
        {
            TextBoxAddServer.Focus();
        }

        private void AddServerPopup_Closed(object sender, EventArgs e)
        {
            TextBoxAddServer.Text = string.Empty;
        }
    }
}